<?php
# Sample script for RAWS tutorial 12: see http://rambla.eu/ for more info.
#
# Copyright (C) 2010 rambla.be

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require_once '../RawsClient/Raws/Rass.php';

# Provide your own RASS credentials here
define('USER', 'xxx'); # your user account name
define('PWD', 'xxx'); # your user account pwd
define('CDN', "cdn0x"); # your sub-cdn (e.g. "cdn01")

define('BASE', 'http://rass.' . CDN . '.rambla.be/');


# all RASS API methods (except HEAD) will raise an exception if they don't return an HTTP SUCCESS CODE (200,201,204)
try {
  
  # Instantiate an object that manages the RASS connection, passing it your login credentials and the base service uri
  $rass = new Rass(USER, PWD, BASE);

  # Manage vocabularies
  echo "Getting feed of existing vocabularies:";
  $feed = $rass->getVocabFeed();
  foreach ($feed as $entry) {
    echo "\n\nName: " . $entry->content->params->name->text;
    echo "\nXML Namespace: " . $entry->content->params->xml_namespace->text;
    echo "\nDescription: " . $entry->content->params->description->text;
  }

  echo "\n\nCreating vocabulary:";
  $entry = $rass->createVocab("megamix", "http://megamix.org/meta/");
  echo "\n\nName: " . $entry->content->params->name->text;
  echo "\nXML Namespace: " . $entry->content->params->xml_namespace->text;
  echo "\nDescription: " . $entry->content->params->description->text;

  echo "\n\nGetting single vocabulary by name:";
  $entry = $rass->getVocabEntry("megamix");
  echo "\n\nName: " . $entry->content->params->name->text;
  echo "\nXML Namespace: " . $entry->content->params->xml_namespace->text;
  echo "\nDescription: " . $entry->content->params->description->text;

  echo "\n\nUpdating existing vocabulary:";
  $entry = $rass->updateVocab("megamix", "http://megamix.net/meta/", "supacatchatroopa");
  echo "\n\nName: " . $entry->content->params->name->text;
  echo "\nXML Namespace: " . $entry->content->params->xml_namespace->text;
  echo "\nDescription: " . $entry->content->params->description->text;
  
  // echo "\n\nDeleting vocabulary.";
  // $entry = $rass->deleteVocab("megamix");

}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Raws::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
